package org.itboys.member;

import org.itboys.commons.CommonConstants;
import org.itboys.commons.utils.ajax.AjaxUtils;
import org.itboys.framework.query.JsonPageUtils;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.fuzion.entity.FuzionMember;
import org.itboys.fuzion.service.FuzionMemberService;
import org.itboys.manager.SellerMemberService;
import org.itboys.mongodb.utils.page.Page;
import org.itboys.mongodb.utils.query.QueryParamUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * 大乱斗的会员管理
 * @author weisky
 *
 */
@RestController 
@RequestMapping("/fuzion/member") 
public class FuzionMemberController extends BaseController{

	@Autowired
	private FuzionMemberService fuzionMemberService;
	@Autowired
	private SellerMemberService sellerMemberService;

	
	/**
	 * 跳到会员管理页面 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/toListVM")
	public ModelAndView toListVM(HttpServletRequest request, HttpServletResponse response) {
		return new ModelAndView("/member/member-list");
	}
	
	@RequestMapping("/list")
	public void list(HttpServletRequest request, HttpServletResponse response){
		Map<String, Object> param = QueryParamUtils.builderQueryMap(request);
		Page<FuzionMember> page = sellerMemberService.listByPage(param, request);
		JsonPageUtils.renderJsonPage(page.getTotalPages(), page.getData(), response);
	} 
	
	
	/**
	 * 保存会员信息
	 * @param member
	 * @param request
	 * @param response
	 */
	@RequestMapping("/save")
	public void save(FuzionMember member, 
			HttpServletRequest request, HttpServletResponse response){
		try{
			sellerMemberService.saveFuzionMember(member);
			AjaxUtils.renderText(response, CommonConstants.SUCCESS);
		}catch(Exception e){
			e.printStackTrace();
			logger.error("MemberController.save err", e);
			AjaxUtils.renderText(response, CommonConstants.FAIL);
		}
	}
	
	/**
	 * 修改会员信息
	 * @param member
	 * @param request
	 * @param response
	 */
	@RequestMapping("/update")
	public void update(FuzionMember member, 
			HttpServletRequest request, HttpServletResponse response){
		try{
			fuzionMemberService.update(member);
			AjaxUtils.renderText(response, CommonConstants.SUCCESS);
		}catch(Exception e){
			e.printStackTrace();
			logger.error("MemberController.save err", e);
			AjaxUtils.renderText(response, CommonConstants.FAIL);
		}
	}
	
	/**
	 * 根据会员的openId获取会员信息
	 * @param request
	 * @param response
	 */
	@RequestMapping("/getByOpenId")
	public void getById(String openId, HttpServletRequest request, HttpServletResponse response){
		FuzionMember fm = fuzionMemberService.getByField("openId", openId);
		fuzionMemberService.setSessionMember(fm.getId(), request, response);//第一步  放入session中
		AjaxUtils.renderJson(response, fuzionMemberService.getByField("openId", openId));
	}
}
